import { initThreeInWorker } from './business-orbitcontrols.js'
import { ElementProxyReceiver } from './ElementProxyReceiver.js'

const receiver = new ElementProxyReceiver()

function start(data) {
  initThreeInWorker({
    canvas: data.canvas,
    inputElement: receiver
  })
}

const handlers = {
  start,
  size: receiver.handleSize.bind(receiver),
  event: receiver.handleEvent.bind(receiver)
}

self.onmessage = function (e) {
  const fn = handlers[e.data.type]
  if (typeof fn !== 'function') {
    throw new Error('no handler for type: ' + e.data.type)
  }

  fn(e.data)
}
